package com.example.wc.logindemo;

import android.app.FragmentManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.wc.logindemo.MyServiceDemo.AlarmReceiver;
import com.example.wc.logindemo.MyServiceDemo.MyService;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;

import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import static android.R.color.black;
import static com.example.wc.logindemo.LoginDemoActivity.ACCESS;

public class EditBlogActivity extends AppCompatActivity {

    public static final String EXTRA_BLOG_ID = "com.example.wc.logindemo.blog_id";
    private static final String DIALOG_HISTORY = "com.example.wc.logindemo.dialog_history";
    public static final String SERVICE_BLOG_ID = "com.example.wc.logindemo.blog_id";
    public static final String SERVICE_BLOG_DATE = "com.example.wc.logindemo.blog_date";
    public static final String SERVICE_EDITBLOGACTIVITY_CONTEXT = "com.example.wc.logindemo.editblogactivity_context";

    private TextView mBack;
    private EditText mName;
    private EditText mContent;
    private Button mEdit;
    private Button mSave;
    private ImageButton mNameImageButton;
    private ImageButton mContentImageButton;
    private SpeechRecognizerDemo mSpeechRecognizerDemo;

    private ImageButton mHistoryImageButton;

    private Blog mBlog;
    private Timer mTimer;

    private MyService.MyBinder myBinder;
    private Intent bindIntent;

    private ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBinder = (MyService.MyBinder) service;
            myBinder.startSaveBlog();
        }
    };

    //传入1/3:newIntent()方法附加extra信息blogId
    public static Intent newIntent(Context packageContext, String blogId){
        Intent intent = new Intent(packageContext, EditBlogActivity.class);
        intent.putExtra(EXTRA_BLOG_ID, blogId);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blog_edit);

        mBack = (TextView) findViewById(R.id.back_title_edit_blog_activity);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EditBlogActivity.this, BlogsListActivity.class);
                startActivity(intent);
            }
        });

        mName = (EditText)findViewById(R.id.name_edit_text_edit_blog_activity);
        mName.setEnabled(false);
        mName.setTextColor(getResources().getColor(black));

//                        //EditText自动换行
//                        //设置EditText的显示方式为多行文本输入
//                        mContent.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
//                        //文本显示的位置在EditText的最上方
//                        mContent.setGravity(Gravity.TOP);
//                        //改变默认的单行模式
//                        mContent.setSingleLine(false);
//                        //水平滚动设置为False
//                        mContent.setHorizontallyScrolling(false);

        mContent =(EditText)findViewById(R.id.content_edit_text_edit_blog_activity);
        mContent.setEnabled(false);
        mContent.setTextColor(getResources().getColor(black));

        mNameImageButton = (ImageButton) findViewById(R.id.speech_name_image_button_edit);
        SpeechUtility.createUtility(this, SpeechConstant.APPID + "=58babddc");
        if(mSpeechRecognizerDemo == null){
            mSpeechRecognizerDemo = new SpeechRecognizerDemo();
        }
        mNameImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSpeechRecognizerDemo.btnVoice(EditBlogActivity.this, mName);
            }
        });

        mContentImageButton = (ImageButton) findViewById(R.id.speech_content_image_button_edit);
        mContentImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSpeechRecognizerDemo.btnVoice(EditBlogActivity.this, mContent);
            }
        });

        mEdit = (Button)findViewById(R.id.edit_button_edit_blog_activity);
        mEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEdit.setVisibility(View.GONE);
                mSave.setVisibility(View.VISIBLE);
                mNameImageButton.setVisibility(View.VISIBLE);
                mContentImageButton.setVisibility(View.VISIBLE);
                mName.setEnabled(true);
                mContent.setEnabled(true);
                setTimerTask();

//                bindIntent.putExtra(SERVICE_BLOG_ID, mBlog.getId());
//                bindIntent.putExtra(SERVICE_BLOG_DATE, mBlog.getDate().toString());
//                bindService(bindIntent, connection, BIND_AUTO_CREATE);

            }
        });

        //传入3/3:获取extra信息
        final String blogId = (String) getIntent().getSerializableExtra(EXTRA_BLOG_ID);
        Log.d("id", blogId);
        Log.d("accessToken", ACCESS);
        String url = "http://www.diyerland.com/diyerland/api/blogById?blogId=" + blogId;
        VolleyRequestDemo volleyRequestDemo = new VolleyRequestDemo();
        volleyRequestDemo.volleyRequestDemo_GET_Blog(EditBlogActivity.this,
                blogId, new VolleyRequestDemo.BlogGetBlogListener(){

                    @Override
                    public void onBlogGetBloged(Blog blog) {
                        mBlog = blog;
                        Log.d("GET_BLOG", mBlog.toString());
                        mName.setText(mBlog.getName());
                        mContent.setText(mBlog.getContent());
                    }
                });

        bindIntent = new Intent(EditBlogActivity.this, MyService.class);
//        bindIntent.putExtra(SERVICE_BLOG_ID, blogId);
        startService(bindIntent);

        mNameImageButton = (ImageButton) findViewById(R.id.speech_name_image_button_edit);
//        mNameImageButton.setVisibility(View.GONE);
        SpeechUtility.createUtility(this, SpeechConstant.APPID + "=58babddc");
        if(mSpeechRecognizerDemo == null){
            mSpeechRecognizerDemo = new SpeechRecognizerDemo();
        }
        mNameImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSpeechRecognizerDemo.btnVoice(EditBlogActivity.this, mName);
            }
        });

        mContentImageButton = (ImageButton) findViewById(R.id.speech_content_image_button_edit);
//        mContentImageButton.setVisibility(View.GONE);
        mContentImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSpeechRecognizerDemo.btnVoice(EditBlogActivity.this, mContent);
            }
        });

        mSave = (Button)findViewById(R.id.save_button_edit_blog_activity);
//        mSave.setVisibility(View.GONE);
        mSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                VolleyRequestDemo volleyRequestDemo = new VolleyRequestDemo();
                volleyRequestDemo.volleyRequestDemo_POST_Edit(EditBlogActivity.this,
                        blogId, mName.getText().toString(), mContent.getText().toString(),
                        new VolleyRequestDemo.BlogPostListener() {
                            @Override
                            public void onBlogPosted() {
                                Intent i = new Intent(EditBlogActivity.this, BlogsListActivity.class);
                                startActivity(i);
                            }
                        });

            }
        });

        mHistoryImageButton = (ImageButton) findViewById(R.id.history_button_edit_blog_activity);
        mHistoryImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                BlogLab.get(EditBlogActivity.this).addBlog(mBlog);
                FragmentManager manager = getFragmentManager();
                List<Blog> blogs = BlogLab.get(EditBlogActivity.this).getSameIdBlogs(mBlog.getId());
                DatePickerFragment dialog = new DatePickerFragment(EditBlogActivity.this, blogs,
                        new DatePickerFragment.DialogBlogGetListener() {
                    @Override
                    public void onDialogBlogGeted(Blog blog) {
                        mName.setText(blog.getName());
                        mContent.setText(blog.getContent());
                    }
                });
//                Log.d("onclickblog", dialog.getBlog().toString());
                dialog.show(manager, DIALOG_HISTORY);
            }
        });


    }

    //设置在10000毫秒后每10000毫秒数据库新增一条数据
    private void setTimerTask() {
        mTimer = new java.util.Timer(true);
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                mBlog.setName(mName.getText().toString());
                mBlog.setContent(mContent.getText().toString());
                mBlog.setDate(new Date());
                BlogLab.get(EditBlogActivity.this).addBlog(mBlog);

//                bindIntent.putExtra(SERVICE_BLOG_ID, mBlog.getId());
//                bindIntent.putExtra(SERVICE_BLOG_DATE, mBlog.getDate().toString());
//                bindService(bindIntent, connection, BIND_AUTO_CREATE);
            }
        }, 1*10*1000, 1*10*1000);
    }

    //插入数据blog到数据库
    @Override
    public void onPause(){
        super.onPause();
        if(mTimer != null){
            mTimer.cancel();
        }

    }
}
